package constant.milk.cashbook.asset;

import java.math.BigDecimal;
import java.text.DecimalFormat;

import constant.milk.cashbook.R;
import constant.milk.cashbook.SuperActivity;
import constant.milk.cashbook.util.MilkUtil;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

public class CountActivity extends SuperActivity
{
	public static final String COUNT_DATA = "MONEY_DATA";
	public static final String COUNT_FLAG = "COUNT_FLAG";
	
	private TextView countTextView;
	
	private ImageButton button1;
	private ImageButton button2;
	private ImageButton button3;
	private ImageButton button4;
	private ImageButton button5;
	private ImageButton button6;
	private ImageButton button7;
	private ImageButton button8;
	private ImageButton button9;
	private ImageButton button0;
	private ImageButton button00;
	private ImageButton buttonDelete;
	
	private Button checkButton;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.count_activity);
		
		setGoneTipButton();
		
		setFindViewId();
		setOnClick();
		setData();
	}

	@Override
	protected void setFindViewId()
	{
		countTextView = (TextView) findViewById(R.id.countInputTextView);
		
		button1 = (ImageButton) findViewById(R.id.countInput1ImageButton);
		button2 = (ImageButton) findViewById(R.id.countInput2ImageButton);
		button3 = (ImageButton) findViewById(R.id.countInput3ImageButton);
		button4 = (ImageButton) findViewById(R.id.countInput4ImageButton);
		button5 = (ImageButton) findViewById(R.id.countInput5ImageButton);
		button6 = (ImageButton) findViewById(R.id.countInput6ImageButton);
		button7 = (ImageButton) findViewById(R.id.countInput7ImageButton);
		button8 = (ImageButton) findViewById(R.id.countInput8ImageButton);
		button9 = (ImageButton) findViewById(R.id.countInput9ImageButton);
		button0 = (ImageButton) findViewById(R.id.countInput0ImageButton);
		button00 = (ImageButton) findViewById(R.id.countInput00ImageButton);
		buttonDelete = (ImageButton) findViewById(R.id.countInputDeleteImageButton);
		
		checkButton = (Button) findViewById(R.id.checkButton);
	}

	@Override
	protected void setOnClick()
	{
		button1.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setTextInput("1");
			}
		});
		
		button2.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setTextInput("2");
			}
		});
		
		button3.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setTextInput("3");
			}
		});
		
		button4.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setTextInput("4");
			}
		});
		
		button5.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setTextInput("5");
			}
		});
		
		button6.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setTextInput("6");
			}
		});
		
		button7.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setTextInput("7");
			}
		});
		
		button8.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setTextInput("8");
			}
		});
		
		button9.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setTextInput("9");
			}
		});
		
		button0.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setTextInput("0");
			}
		});
		
		button00.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setTextInput("00");
			}
		});
		
		buttonDelete.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setTextDelete();
			}
		});
		
		checkButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent();
				intent.putExtra(COUNT_DATA, countTextView.getText().toString());
				setResult(Activity.RESULT_OK, intent);
				finish();
			}
		});
	}

	@Override
	protected void setData()
	{
		Intent intent = getIntent();
		String count = intent.getStringExtra(COUNT_DATA);
		int flag = intent.getIntExtra(COUNT_FLAG, 0);
		
		if (flag == 0)
		{
			setTitle("우선순위");
		}
		else if (flag == 1)
		{
			setTitle("갯수");
		}
		else if (flag == 2)
		{
			setTitle("할부개월");
		}
		else if (flag == 3)
		{
			setTitle("평수");
		}
		
		countTextView.setText(count);
	}
	
	private void setTextInput(String number)
	{
		String text = countTextView.getText().toString();
		
		text = text + number;
		
		if (getThreeLengthCheck(text))
		{
			MilkUtil.showToast(superContext, "3자리 이상 할 수 없습니다.");
			return;
		}
		
		text = makeStringWithComma(text);
		
		countTextView.setText(text);
	}
	
	private void setTextDelete()
	{
		String text = countTextView.getText().toString();
		
		if (text.length() < 2)
		{
			countTextView.setText("0");
			return;
		}
		
		text = text.substring(0, text.length()-1);
		
		text = makeStringWithComma(text);
		
		countTextView.setText(text);
	}
	
	public boolean getThreeLengthCheck(String text)
	{
		if (text.length() > 3)
		{
			return true;
		}
		
		return false;
	}
	
	public String makeStringWithComma(String name)
	{
		String string = name;
		
		if (string.length() == 0)
		{
			return "";
		}
		
		try
		{
			BigDecimal value = new BigDecimal(string);
			
			
			DecimalFormat format = new DecimalFormat("###");
			return format.format(value);    
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
		return string;
	}

	@Override
	protected void tipButton()
	{
	}
}
